// pages/report/pdf/pdf.js
const api=require('../../../utils/request')
Page({

  /**
   * 页面的初始数据
   */
  data: {
   pdf:'',
   filePath:"",
   isShow:false,
   shareModel:{
    title:'',
    path:'pages/test/test', 
    fromButton:'one', 
  }, 
  fileName:'',
  name:'',
  posterId:'',
  imageList:[],
  pageNum:1,
  pageSize: 5,
  fileId:'',
  showDialog: false,
  catchtouchmove: true,
  memberId:'',
  reportId:'',
  pdfPath:'',
  isPdf: false,
  showLogin: false,
  image:'',
  backType:''
   },
     //联系客服
  kf(){
    //
//    wx.openCustomerServiceChat({
//        extInfo: {url: 'https://work.weixin.qq.com/kfid/kfcdb659362f1afb3c0'},
//        corpId: 'wwbb8c2c0fafdbf12b',
//        success(res) {
//            console.log('resss',res)
//        }
//      })

wx.navigateTo({
    url: '/pages/vip/vip_recommend/vip_recommend',
  })
  },
  //显示菜单
  showMenu(){
    this.setData({
      isShow:true
    });
  },
  switch(){
    this.setData({
      catchtouchmove:!this.data.catchtouchmove
    })
  },

  //隐藏菜单
  hideMenu(){
    this.setData({
      isShow:false
    });
  },
  onBackPress: function(options){ 
     wx.navigateBack({
        delta: 1, // 返回的页面数，如果 delta 大于现有页面数，则返回到首页
        success: function(res) {
          // 返回成功后，重新加载页面
          wx.redirectTo({
            url: '当前页面路径' // 替换为当前页面的路径
          });
        }
      });
      return true; // 阻止小程序默认的返回行为
    
  },
  save_report(){
    this.setData({
        isShow: false
    })
  wx.navigateTo({
    url: '/pages/report/save_report/save_report?posterId='+this.data.posterId,
  })
},
copy(data){
    let reportId= this.data.posterId 
    
    wx.setClipboardData({
      data: 'https://www.xinyanbao.cn/#/pdf/?id='+reportId,
      success:function(res){ 

      }
    })
 },
 downloadFile(){
    // wx.showToast({
    //     title: '登录电脑端下载',
    //     icon: 'none',
    //     duration: 2000,
    //   })
    //wx.showLoading()
    let reportId=this.data.posterId
    let userInfo= wx.getStorageSync('user')
    let user= JSON.parse(userInfo)  
    let param={memberId:user.id,reportId:reportId}
    api.get('weChatMini/downloadDay/'+user.id).then(response=>{
        if(response.code===200){
            wx.showLoading({
                title: '下载中', // 这里是加载提示的文字
              })
            let that=this
          wx.downloadFile({
                url: this.data.pdfPath,
                filePath: wx.env.USER_DATA_PATH+'/'+that.data.fileName,
                 success: function (res) { 
                     that.setData({
                        isShow: false
                     })
                    var Path = wx.env.USER_DATA_PATH+'/'+that.data.fileName //返回的文件临时地址，用于后面打开本地预览所用
                    wx.openDocument({
                        filePath: Path,
                        fileType: 'pdf',
                        showMenu: true, 
                        success: function () { 
                            wx.hideLoading()
                            api.post('weChatMini/saveDownloadData',param).then(res=>{})    
                        }
                    })
                },
                fail: function (res) { 
                    // wx.showToast({
                    //   title: '下载失败',
                    //   success:'error'
                    // })
                }
            })
        }else{
             wx.showToast({
                      title: response.msg,
                      success:'error'
                    })
        }
    })

   

 },
 pc(){
    wx.navigateTo({
        url: '/pages/mine/login_pc/login_pc',
      })
 },
 saveCollect(){
    let userInfo= wx.getStorageSync('user')
    let user= JSON.parse(userInfo) 
    let reportId=this.data.posterId
    let that=this
    api.post('weChatMini/saveCollect',{memberId: user.id,reportId:reportId}).then(res=>{
        if(res.code===200){
            wx.showToast({
                title: '收藏成功',
                icon: 'none',
                duration: 2000,
              })
              that.setData({
                isShow: false
              })
        }
    })
 },

 login(){
     wx.navigateTo({
        url: '/pages/login/login?type=3&id='+this.data.reportId+'&backType='+this.data.backType,
      })
 },
 // 查看pdf事件方法  
 pdf_open(id) {

//   wx.showLoading({
//       title: '加载中',
//   })
  let that=this
   
  
  api.get('weChatMini/findReportById/'+id).then(response=>{ 
      if(response.code===200){ 
          const systemInfo = wx.getSystemInfoSync();
const system = systemInfo.system;
 let pdf=''
if (system.indexOf('Android') !== -1) {
    pdf='https://xinyanbao.cn/pdfJS/web/viewer.html?file='+response.data.path
  }else{
      pdf= response.data.path
  }
  wx.setStorageSync('pdf', pdf)  

        that.setData({
            pdf:pdf,
            //pdf: 'https://xinyanbao.cn/pdfJS/web/viewer.html?file='+response.data.path,
            name: response.data.name,
            posterId: response.data.id,
            fileName: response.data.fileName ,
            fileId: response.data.fileId,
            pdfPath: response.data.path,
            isPdf: true,
            image: response.data.image
           })
           let userInfo= wx.getStorageSync('user')
           if(userInfo===''){
              that.setData({
                showLogin: true
              })
           }else{
            api.post('web/readDayNum',{memberId:that.data.memberId,reportId:that.data.posterId}).then(read=>{
                if(read.code!==200){
                  that.setData({
                   showDialog: true
                  })
                  
               }else{
                   that.saveBrowse(that.data.posterId)
                  
               }
       })
           }
           wx.hideLoading()
        wx.setNavigationBarTitle({
         title: response.data.name
     })

      }
     

    
      
      
    
  })
  // 下载文件方法
  
},
loadImage(type){
    let that=this;
    api.post('weChatMini/findPDFImage',{id: this.data.fileId,pageNum: this.data.pageNum,pageSize:this.data.pageSize,path:this.data.pdf}).then(result=>{
         if(result.code===200){
            if(type===2){
                let data=that.data.imageList.concat(result.data.records)
                that.setData({
                    imageList: data,
                    pages: result.data.pages
                })
            }else{
                that.setData({
                    imageList: result.data.records,
                    pages: result.data.pages
                })
            }

            api.post('web/readDayNum',{memberId:that.data.memberId,reportId:that.data.posterId}).then(read=>{
                
                    if(read.code!==200){
                       that.setData({
                        showDialog: true
                       })
                    }else{
                        that.saveBrowse(that.data.posterId)
                    }
            })
            
        }
        //this.saveBrowse(options.id)
       
        wx.hideLoading()
    })

},
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let userInfo= wx.getStorageSync('user')
    if(userInfo!==''){
     let user= JSON.parse(userInfo)
     this.setData({
         memberId: user.id
         
     })
     }
     this.setData({
        reportId: options.id
     })
     this.pdf_open(options.id)
     this.setData({
        backType: options.backType
     })
     
      
  },
  saveBrowse(id){
    let userInfo= wx.getStorageSync('user')
    if(userInfo!==''){
     let user= JSON.parse(userInfo)
     api.post('weChatMini/saveBrowse',{reportId:id,memberId:user.id}).then()
    }
      
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
   // this.pdf_open();
     
    },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
        pdf: ''
    })
    let pdf=wx.getStorageSync('pdf')  
    this.setData({
        pdf: pdf
    })
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },
  handleReturn: function(e){
   },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
     
    wx.navigateBack({
        delta: 1 
      });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.pages>this.data.pageNum){
        this.setData({
            pageNum: this.data.pageNum+1
        })
        this.loadImage(2)
    } 
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(res) {
    if(res.from==='button'){
        this.setData({
            FX: false
        })
     
    }
    return {
        title: this.data.name,
        path:'/pages/report/pdf/pdf?id='+this.data.posterId,
        imageUrl: this.data.image
      }
  },
  onShareTimeline(res){
    return {
        title: '',
        imageUrl: this.data.image,
        query:{}
    }

  },
   
})